package com.sky.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.mybatis.mp.core.mybatis.mapper.context.Pager;
import com.sky.dto.GetBorroweuestsDTO;
import com.sky.dto.UpBorroweuestsStatusDTO;
import com.sky.result.Result;
import com.sky.service.BorrowrequestsService;
import com.sky.service.UserBorrowhistoryService;
import com.sky.vo.GetBorroweuestsVO;
import com.sky.vo.GetUserBorrowVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 借阅管理相关接口
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminBorrowController {

    @Autowired
    private BorrowrequestsService borrowrequestsService;

    @Autowired
    private UserBorrowhistoryService userBorrowhistoryService;

    /**
     * 获取借阅申请信息分页查询
     *
     * @param getBorroweuestsDTO 分页查询参数
     * @return 借阅申请信息
     * @角色 BorrowingAdmin，SuperAdmin
     * @权限 BorrowingManagementRelated
     */
    @SaCheckPermission(value = {"BorrowingManagementRelated"}, orRole = {"BorrowingAdmin", "SuperAdmin"})
    @PostMapping("/getBorroweuests")
    public Result<Pager<GetBorroweuestsVO>> getBorroweuests(@RequestBody GetBorroweuestsDTO getBorroweuestsDTO) {

        log.info("获取借阅申请表:{}", getBorroweuestsDTO);

        Pager<GetBorroweuestsVO> getBorroweuestsVOPager = borrowrequestsService.getBorroweuests(getBorroweuestsDTO);

        return Result.success(getBorroweuestsVOPager);

    }

    /**
     * 借阅申请状态改变
     *
     * @param upBorroweuestsStatusDTO 借阅申请状态修改参数
     * @return Result
     * @角色 BorrowingAdmin，SuperAdmin
     * @权限 BorrowingManagementRelated
     */
    @SaCheckPermission(value = {"BorrowingManagementRelated"}, orRole = {"BorrowingAdmin", "SuperAdmin"})
    @PostMapping("/upBorroweuestsStatus")
    public Result upBorroweuestsStatus(@RequestBody UpBorroweuestsStatusDTO upBorroweuestsStatusDTO) {

        log.info("借阅申请状态改变：{}", upBorroweuestsStatusDTO);

        borrowrequestsService.upBorroweuestsStatus(upBorroweuestsStatusDTO);

        return Result.success();

    }

    /**
     * 查询用户借阅历史信息
     *
     * @param getBorroweuestsDTO 分页查询参数
     * @return 用户借阅历史信息
     * @角色 BorrowingAdmin，SuperAdmin
     * @权限 BorrowingManagementRelated
     */
    @SaCheckPermission(value = {"BorrowingManagementRelated"}, orRole = {"BorrowingAdmin", "SuperAdmin"})
    @PostMapping("/getUserBorrow")
    public Result<Pager<GetUserBorrowVO>> getUserBorrow(@RequestBody GetBorroweuestsDTO getBorroweuestsDTO) {

        log.info("查询用户借阅历史信息：{}", getBorroweuestsDTO);

        Pager<GetUserBorrowVO> getUserBorrowVOPager = userBorrowhistoryService.getUserBorrow(getBorroweuestsDTO);

        return Result.success(getUserBorrowVOPager);

    }

    /**
     * 发送逾期邮件
     *
     * @param borrowHistoryId 用户借阅历史订单ID
     * @return result
     * @角色 BorrowingAdmin，SuperAdmin
     * @权限 BorrowingManagementRelated
     */
    @SaCheckPermission(value = {"BorrowingManagementRelated"}, orRole = {"BorrowingAdmin", "SuperAdmin"})
    @PostMapping("/sendOverdueEmail")
    public Result sendOverdueEmail(String borrowHistoryId) {

        log.info("发送逾期邮件：{}", borrowHistoryId);

        userBorrowhistoryService.sendOverdueEmail(borrowHistoryId);

        return Result.success();

    }

    /**
     * 归还图书
     *
     * @param borrowHistoryId 借阅历史记录ID
     * @return Result
     * @角色 BorrowingAdmin，SuperAdmin
     * @权限 BorrowingManagementRelated
     */
    @SaCheckPermission(value = {"BorrowingManagementRelated"}, orRole = {"BorrowingAdmin", "SuperAdmin"})
    @PostMapping("/return-book")
    public Result returnBook(@RequestParam String borrowHistoryId) {
        log.info("归还图书：{}", borrowHistoryId);
        userBorrowhistoryService.returnBook(borrowHistoryId);
        return Result.success();
    }
} 